import { Request } from './../../interfaces/request.interface';
import { JwtAuthGuard } from './../../guards/jwt-auth.guard';
import { WxMiniLoginDto } from './dto/wxMiniLogin.dto';
// import { Request } from 'express';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { LoginDto } from './dto/login.dto';
import { UserEntity } from './../../entity/user.entity';
import { CreateUserDto } from './dto/createUser.dto';
import { Body, Controller, Post, UseGuards, Get, Req, UseInterceptors, ClassSerializerInterceptor, Inject, LoggerService } from '@nestjs/common';
import { UserService } from './user.service';
import { ApiBearerAuth, ApiHeader, ApiHeaders, ApiOperation, ApiTags } from '@nestjs/swagger';
import { NotAuthorized } from 'src/decorator/roles.decorator';


@ApiHeader({
    name: 'token',
})
@ApiTags('用户')
@Controller('user')
export class UserController {
    constructor(
        @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService,
        private readonly userServi: UserService
    ) { }


    @ApiOperation({ summary: '小程序登录' })
    @Post('wxMiniLogin')
    wxMiniLogin(@Body() wxloginDto: WxMiniLoginDto): any {
        console.log("🚀 ~ file: user.controller.ts ~ line 21 ~ UserController ~ wxMiniLogin ~ wxloginDto", wxloginDto)
        return this.userServi.wxMinilogin(wxloginDto);
    }

    /**
     *  普通账号密码登陆 
     */

    @ApiOperation({ summary: '账号密码登录' })
    @NotAuthorized()
    @Post('login')
    login(@Body() loginDto: LoginDto): any {
        this.logger.error(loginDto)
        return this.userServi.login(loginDto);
    }

    @ApiOperation({ summary: '账号注册' })
    @NotAuthorized()
    @Post('register')
    addUser(@Body() createUserDto: CreateUserDto): any {
        return this.userServi.addOne(createUserDto);
    }

    @ApiOperation({ summary: '获取账号信息' })
    @Get('profile')
    async getProfile(@Req() req: Request): Promise<UserEntity> {
        return this.userServi.findById(req.user.id)
    }

    @ApiOperation({ summary: '获取验证码' })
    @NotAuthorized()
    @Get('getCaptcha')
    async getCaptcha(): Promise<any> {
        return this.userServi.genCaptcha();
    }
} 
